// pages/my/my_account/index.js
const account=require("../../controllers/ControllerX").controller.account;
const accountList=require("../../controllers/ControllerX").controller.accountList;

const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    accountData1:{},
    accountData2:{},
    accountData3:{},
    select1:true,
    select2:false,
    select3:false,
    select4:true,
    select5:false,
    //收入数据
    inputState:true,
    outputState:false,
    //展现的数据
    selectShow:[],
    selectShow2:[],
    value:{
      pageNumber:1,
      pageSize:20,
      genre:0,
      shopId:"",
      type:'',
      lowLine:'',
      startTime:'',
      endTime:'',
    },
    lastPage:false,
    stateItem:"",
    shopImg:'',
    profitMoney1:"",
    profitMoney2:"",
    profitMoney3:"",
    incomeOrCost:['收入','支出'],
    incomeList:['全部','服务订单','商品订单','云商品订单','会员充值','线下收入','爆品收益','欢乐拼','限时抵扣','超值礼包'],
    costList:['全部','线下支出','提现','员工结算','门店续费'],
    firstIndex:0,
    secondIndex:0,
    thiredIndex:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      'value.shopId':wx.getStorageSync('shopId')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAccount();
    this.getAccountList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getMore();
  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // },
  // 获取店铺收入支出余额数据
  getAccount(){
    var shopId=wx.getStorageSync('shopId');
    //全部收入支出
    account({shopId:shopId}).then(res=>{
      this.setData({
        accountData1:res.data,
        profitMoney1:(res.data.income-res.data.pay).toFixed(2)
      })
    })
    //线上收入支出
    account({shopId:shopId,type:0}).then(res=>{
      this.setData({
        accountData2:res.data,
        profitMoney2:(res.data.income-res.data.pay).toFixed(2)
      })
    })
    //线下收入支出
    account({shopId:shopId,type:1}).then(res=>{
      this.setData({
        accountData3:res.data,
        profitMoney3:(res.data.income-res.data.pay).toFixed(2)
      })
    })
  },
  //进行tab选择
  selectTab(event){
    let state=event.currentTarget.dataset.state;
    if(state=="0"){
      this.setData({
        select1:true,
        select2:false,
        select3:false
      })
    }else if(state=="1"){
      this.setData({
        select1:false,
        select2:true,
        select3:false
      })
    }else{
      this.setData({
        select1:false,
        select2:false,
        select3:true
      })
    }
  },
  //跳转提现记录
  jumpToInputHistory(){
    wx.navigateTo({
      url: '/pages/my/my_moneyInputHistory/index',
    })
  },
  //收入支出切换
  selectOutAndInput(event){
    let index=event.currentTarget.dataset.index;
    this.setData({
      firstIndex:index
    })
    let value=this.data.value
    if(index===0){
      value.genre=0
      value.type=''
      value.lowLine=''
      this.setData({
        secondIndex:0
      })
    }else{
      value.genre=1
      value.type=""
      value.lowLine=""
      this.setData({
        thiredIndex:0
      })
    }
    this.setData({
      value:value
    })
    this.getAccountList()
  },

  getAccountList(){
    accountList(this.data.value).then(res=>{
      this.setData({
        showList:res.data.page.list,
        lastPage:res.data.page.lastPage
      })
    })
  },

  //类型切换展现数据
  selectSeTab(e){
    let firstIndex=this.data.firstIndex
    let index=e.currentTarget.dataset.index;
    let value=this.data.value
    if(firstIndex===0&&index===0){
        value.genre=0
        value.type=''
        value.lowLine=''
        this.setData({
          secondIndex:index
        })
    }else if(firstIndex===0&&index===1){
      value.genre=0
      value.type=6
      value.lowLine=0
      this.setData({
        secondIndex:index
      })
    }else if(firstIndex===0&&index===2){
      value.genre=0
      value.type=1
      value.lowLine=0
      this.setData({
        secondIndex:index
      })
    }else if(firstIndex===0&&index===3){
      value.genre=0
      value.type=7
      value.lowLine=0
      this.setData({
        secondIndex:index
      })
    }else if(firstIndex===0&&index===4){
      value.genre=0
      value.type=0
      value.lowLine=0
      this.setData({
        secondIndex:index
      })
    }else if(firstIndex===0&&index===5){
      value.genre=0
      value.type=''
      value.lowLine=1
      this.setData({
        secondIndex:index
      })
    }else if(firstIndex===0&&index===6){
      value.genre=0
      value.type=12
      value.lowLine=0
      this.setData({
        secondIndex:index
      })
    }else if(firstIndex===0&&index===7){
      value.genre=0
      value.type=20
      value.lowLine=0
      this.setData({
        secondIndex:index
      })
    }else if(firstIndex===0&&index===8){
      value.genre=0
      value.type=22
      value.lowLine=0
      this.setData({
        secondIndex:index
      })
    }else if(firstIndex===0&&index===9){
      value.genre=0
      value.type=23
      value.lowLine=0
      this.setData({
        secondIndex:index
      })
    }else if(firstIndex===1&&index===0){
      value.genre=1
      value.type=""
      value.lowLine=""
      this.setData({
        thiredIndex:index
      })
    }else if(firstIndex===1&&index===1){
      value.genre=1
      value.type=9
      value.lowLine=1
      this.setData({
        thiredIndex:index
      })
    }else if(firstIndex===1&&index===2){
      value.genre=1
      value.type=5
      value.lowLine=0
      this.setData({
        thiredIndex:index
      })
    }else if(firstIndex===1&&index===3){
      value.genre=1
      value.type=2
      value.lowLine=0
      this.setData({
        thiredIndex:index
      })
    }else if(firstIndex===1&&index===4){
      value.genre=1
      value.type=8
      value.lowLine=0
      this.setData({
        thiredIndex:index
      })
    }
    this.setData({
      value:value
    })
    this.getAccountList()
  },

  //跳转提现
  jumpMoneyPut(){
    wx.navigateTo({
      url: '/pages/my/my_moneyPut/index',
    })
  },

  getMore(){
   if(this.data.lastPage==false){
    let value=this.data.value
    value.pageNumber=value.pageNumber+1
    this.setData({
      value:value
    })
    accountList(value).then(res=>{
      let list=this.data.showList
      let list2=res.data.page.list
      let list3=list.concat(list2)
      this.setData({
        showList:list3,
        lastPage:res.data.page.lastPage
      })
    })
   }
  },

   //改变开始时间
   bindStartChange(e){
    this.setData({
      'value.startTime':e.detail.value
    })
    this.getAccountList();
  },

  //改变结束时间
  bindEndChange(e){
    this.setData({
      'value.endTime':e.detail.value
    })
    this.getAccountList();
  }
})